package com.zc.timedown.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.List;
import com.zc.timedown.domain.TimeRecord;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

/**
 * 时间账本Mapper接口
 *
 * @author zc
 * @date 2025-10-21
 */
@Mapper
public interface TimeRecordMapper extends BaseMapper<TimeRecord>
{
    /**
     * 查询时间账本
     *
     * @param id 时间账本主键
     * @return 时间账本
     */
    public TimeRecord selectTimeRecordById(Long id);

    /**
     * 查询时间账本列表
     *
     * @param timeRecord 时间账本
     * @return 时间账本集合
     */
    public List<TimeRecord> selectTimeRecordList(TimeRecord timeRecord);

    /**
     * 新增时间账本
     *
     * @param timeRecord 时间账本
     * @return 结果
     */
    public int insertTimeRecord(TimeRecord timeRecord);

    /**
     * 修改时间账本
     *
     * @param timeRecord 时间账本
     * @return 结果
     */
    public int updateTimeRecord(TimeRecord timeRecord);

    /**
     * 删除时间账本
     *
     * @param id 时间账本主键
     * @return 结果
     */
    public int deleteTimeRecordById(Long id);

    /**
     * 批量删除时间账本
     *
     * @param ids 需要删除的数据主键集合
     * @return 结果
     */
    public int deleteTimeRecordByIds(Long[] ids);


    /**
     * 根据用户ID和日期查询当天总时间
     */
    @Select("SELECT SUM(duration_hours) FROM time_record WHERE user_id = #{userId} AND record_date = #{recordDate}")
    BigDecimal getTotalHoursByUserIdAndDate(@Param("userId") Long userId, @Param("recordDate") LocalDate recordDate);


    /**
     * 查询指定年月的时间记录
     */
    @Select("SELECT id, user_id, activity_name, duration_hours, record_date, description ,create_time FROM time_record " +
            "WHERE user_id = #{userId} " +
            "AND YEAR(record_date) = #{year} " +
            "AND MONTH(record_date) = #{month} " +
            "ORDER BY record_date, id")
    List<TimeRecord> selectMonthlyRecords(@Param("userId") Long userId,
                                          @Param("year") Integer year,
                                          @Param("month") Integer month);

    @Select("SELECT id, user_id, activity_name, duration_hours, record_date, description,create_time FROM time_record " +
            "WHERE user_id = #{userId} " +
            "AND record_date BETWEEN #{weekStart} AND #{weekEnd} " +
            "ORDER BY activity_name, record_date")
    List<TimeRecord> selectWeeklyRecordsByCategory(@Param("userId") Long userId,
                                                   @Param("weekStart") LocalDate weekStart,
                                                   @Param("weekEnd") LocalDate weekEnd);

    /**
     * 根据日期和用户ID查询时间记录
     * @param userId 用户ID
     * @param date 日期
     * @return 时间记录列表
     */
    @Select("SELECT id, user_id, activity_name, duration_hours,record_date, description, create_time, update_time " +
            "FROM time_record " +
            "WHERE user_id = #{userId} " +
            "AND record_date = #{date} " +
            "ORDER BY create_time")
    List<TimeRecord> selectByDateAndUser(@Param("userId") Long userId,
                                         @Param("date") LocalDate date);

   List<TimeRecord> selectTimeRecordsByDate(@Param("userId") Long userId, @Param("date") LocalDate date);

}